<script>
import { getToken, getSchoolInfo, getUserInfo } from "@/utils/author";
import store from "@/store";

export default {
  onLaunch: function () {
    const token = getToken();
    const schoolInfo = getSchoolInfo();
    const userInfo = getUserInfo();
    if (token && schoolInfo && userInfo) {
      const { schoolId, schoolName, areaCode } = JSON.parse(schoolInfo);
      const { openId, nickName, avatarUrl } = JSON.parse(userInfo);
      store.commit("SET_TOKEN", token);
      store.commit("SET_OPEN_ID", openId);
      store.commit("SET_NICK_NAME", nickName);
      store.commit("SET_AVATAR_URL", avatarUrl);
      store.commit("SET_SCHOOL_ID", schoolId);
      store.commit("SET_SCHOOL_NAME", schoolName);
      store.commit("SET_AREA_CODE", areaCode);
      return;
    }
    this.$store
      .dispatch("Login")
      .then((result) => {
        uni.showToast({
          title: "登录成功!",
          icon: "success",
          duration: 2000,
        });
        if (!result) {
          store.commit("SET_SCHOOL_ID", "");
          store.commit("SET_SCHOOL_NAME", "");
          store.commit("SET_AREA_CODE", "");
          return;
        }
        this.$store.dispatch("getSchoolInfo", result);
      })
      .catch((err) => {
        uni.showToast({
          title: "登录失败!",
          icon: "error",
          duration: 2000,
        });
      });
  },
  onShow: function () {
    //console.log("App Show");
  },
  onHide: function () {
    //console.log("App Hide");
  },
};
</script>

<style lang="scss">
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
@import "uview-ui/index.scss";

@import "./static/icon/iconfont/iconfont.css";

/*每个页面公共css */
page {
  height: 100%;
}
/* uni-page-body{
  height: 100%;
} */
</style>
